<?php
namespace admin;
use models\BaseDao;
class Aclass extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','aclass');
    }
/**
 * 文章分类列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();
        // 获取全部文章分类,按照ord排序
        $data = $db->select('class','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        // 将数据全部分给模板
        $this->assign('data',$data);

        //标题    
        $this->assign('title','文章分类');
        // 导入模板
        $this->display('aclass/index');
    }
    function add(){
        // 如果$_POST['do_submit]存在,说明是添加动作
        if(isset($_POST['do_submit'])){
            $db = new BaseDao();
            unset($_POST['do_submit']);
            if($db->insert('class',$_POST)){
                $this->success('/admin/aclass','添加ok');
            }else{
                $this->error('/admin/aclass/add','添加fail');
            }
        }
        //标题    
        $this->assign('title','添加文章分类');
        $this->display('aclass/add');

    }
    function mod($id){
        $db = new BaseDao();
        $this->assign($db->get('class','*',['id'=>$id]));
        $this->assign('title',"修改文章分类");
        $this->display('aclass/mod');
    }
    function doupdate(){
        $id = $_POST['id'];
        unset($_POST['id']);
        $db = new BaseDao();

        if($db->update('class',$_POST,['id'=>$id])){
            $this->success('/admin/aclass','修改ok');
        }else{
            $this->error('/admin/aclass/mod/'.$id,'修改fail');
        }
    }
    function del($id){
        $db = new BaseDao();
        if($db->delete('class',['id'=>$id])){
            $this->success('/admin/aclass','删除ok');
        }else{
            $this->error('/admin/aclass','删除fail');
        }
    }
    function order(){
        $db = new BaseDao();
        $num = 0;
        foreach($_POST['ord'] as $id=>$ord){
            
            $num += $db->update('class',['ord'=>$ord],['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/aclass','排序ok');
        }else{
            $this->success('/admin/aclass','排序fail');
        }
    }
}